<template>
  <div class="app-container">
    <div class="qrCodeContainer" style="width: 450px; height: 50px; display: none; width: 100%">
      <h1 class="title" style="width: 480px; margin-left: 27%">{{ $t(`Print.RongyangGarmentCoLtdWarehousingList`) }}</h1>
      <div style="width: 95%; margin: auto">
        <div style="display: flex; width: 100%; font-size: 13px; justify-content: space-between">
          <h2 style="margin-left: 70px; font-size: 13px">{{ $t(`Print.InboundOrderNumber`) }}:{{ slipNoList3[0]?.slipNo }}</h2>
          <h2 style="margin-left: 70px; font-size: 13px">{{ $t(`Print.StorageDate`) }}:{{ slipNoList3[0]?.slipDate }}</h2>
          <h2 style="margin-left: 70px; font-size: 13px">{{ $t(`Print.InboundDepartment`) }}:{{ slipNoList3[0]?.vendorName }}</h2>
          <h2 style="margin-left: 70px; font-size: 13px">{{ $t(`Print.WarehousingReason`) }}:{{ slipNoList3[0]?.slipReason }}</h2>
          <h2 style="margin-left: 70px; font-size: 13px">{{ $t(`Print.TabulatedDate`) }}:{{ formatCurrentDateTime(currentDate) }}</h2>
        </div>

        <div style="width: 100%">
          <table style="width: 100%; font-size: 13px">
            <thead>
              <tr>
                <th>{{ $t(`Print.Department`) }}</th>
                <th>{{ $t(`Print.MaterialColorKey`) }}</th>
                <th>{{ $t(`Print.MaterialName`) }}</th>
                <th>{{ $t(`Print.Specification`) }}</th>
                <th>{{ $t(`Print.Unit`) }}</th>
                <th>{{ $t(`Print.Quantity`) }}</th>
                <th>{{ $t(`Print.Directives`) }}</th>
                <th>{{ $t(`Print.Remark`) }}</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(iten1, index) in slipNoList3" :key="index">
                <td>{{ iten1.deptName }}</td>

                <td>{{ iten1.mtlColor }}</td>
                <td>{{ iten1.mtlName }}</td>
                <td>{{ iten1.mtlSpec }}</td>
                <td>{{ iten1.mtlUnit }}</td>

                <td>{{ iten1.qty }}</td>
                <td>{{ iten1.lotNo }}</td>

                <td>{{ iten1.remark }}</td>
                <!-- 更多的数据单元格 -->
              </tr>
              <!-- 更多的行 -->
            </tbody>
            <!-- 可选的 tfoot 部分 -->
          </table>

          <!-- <Table :tableHeader="tableHeader5" :tableData="slipNoList" /> -->
        </div>

        <div style="display: flex; justify-content: space-between; width: 85%; font-size: 17px; width: 80%; margin: auto">
          <h2 style="font-size: 13px">{{ $t(`Print.Manufacturers`) }}:</h2>
          <h2 style="font-size: 13px">{{ $t(`Print.Purchaser`) }}:</h2>
          <h2 style="font-size: 13px">{{ $t(`Print.OrderCreator`) }}:{{ userInfo?.chiName }}</h2>
        </div>
      </div>
    </div>

    <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList1" @Launchform="Launchform" @findClicked="findClicked" />
    <Find :OriginalFilter="{ static: true }" :formList="formList5" @Launchform="Launchform5" @findClicked="findClicked" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <div>
        <div>
          <h1>{{ $t(`Print.DetailsPendingForStorage`) }}</h1>
          <Table
            :tableHeader="tableHeader"
            :tableData="TableListDetailed"
            :TableHeight="50"
            :outTableHeight="90"
            :isLoading="isLoading"
            @selectList="clicktogetdata1"
            @inputChange="confirmChange"
            :getTableList="DetailedMethodOfTapeEntry"
            @currentRow2="ChangeInputField"
          />
        </div>

        <div style="margin-top: 10px">
          <h1>{{ $t(`Print.DetailsOfOtherMaterialsInventoryEntry`) }}</h1>
          <Table
            :tableHeader="tableHeader1"
            :tableData="TableDateListOne.dtos"
            :TableHeight="50"
            :outTableHeight="90"
            :isLoading="isLoading1"
            @currentRow="clicktogetdata5"
          />
        </div>
      </div>
      <PageModule :PageData="{ total }" @pageChange="handleSizeChangeT" @pageSizeChange="handleCurrentChangeT" />
    </el-card>

    <!-- 通用材料 -->
    <el-drawer v-model="drawer1" :direction="'ttb'" size="small" :with-header="false">
      <h1 style="font-size: 16px; margin-bottom: 20px">{{ $t(`Print.GeneralMaterials`) }}</h1>
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList" @Launchform="Launchform1" @findClicked="findClicked1" />
      <h1 style="font-size: 16px; margin-bottom: 20px; margin-top: 15px">{{ $t(`Print.DetailedMaterials`) }}</h1>
      <Table
        @currentRow1="currentRow1"
        style="width: 60%"
        :tableHeader="tableHeader2"
        :tableData="TableDateListGeneral.dtos"
        :TableHeight="75"
        :outTableHeight="50"
        :isLoading="isLoading2"
      />
      <PageModule :PageData="{ total: totalGeneral }" @pageChange="handleSizeChangeTGeneral" @pageSizeChange="handleCurrentChangeTGeneral" />
    </el-drawer>

    <!-- 指令资料 -->
    <el-drawer v-model="drawer" :direction="'ttb'" size="small" :with-header="false">
      <h1 style="font-size: 16px; margin-bottom: 20px">{{ $t(`Print.InstructionalMaterials`) }}</h1>
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList2" @Launchform="Launchform2" @findClicked="findClicked2" />
      <h1 style="font-size: 16px; margin-bottom: 10px; margin-top: 15px">{{ $t(`Print.DetailedMaterials`) }}</h1>
      <Table
        @currentRow1="currentRow1"
        :tableHeader="tableHeader3"
        :tableData="TableDateListInstructionMaterialMethod.dtos"
        :TableHeight="45"
        :outTableHeight="100"
        :isLoading="isLoading3"
        @currentRow="clicktogetdata"
      />
      <PageModule :PageData="{ total: totalLot }" @pageChange="handleSizeChangeTtotalLot" @pageSizeChange="handleCurrentChangeTtotalLot" />
      <h1 style="font-size: 16px; margin-bottom: 10px; margin-top: 15px">{{ $t(`Print.DetailedInformationOnTheAdheringMaterials`) }}</h1>
      <Table
        :tableHeader="tableHeader5"
        :tableData="FitSubmaterialDetailsTableDateList.dtos"
        :TableHeight="45"
        :outTableHeight="100"
        :selection="true"
        @currentRow1="currentRow1"
      />
    </el-drawer>

    <!-- 通用材料 -->
    <el-drawer v-model="drawer2" :direction="'ttb'" size="small" :with-header="false">
      <h1 style="font-size: 16px; margin-bottom: 20px">{{ $t(`Print.GeneralMaterials`) }}</h1>
      <Find :OriginalFilter="{ static: true }" :searchImg="false" :formList="formList3" @Launchform="Launchform3" @findClicked="findClicked3" />
      <Table
        @currentRow1="currentRow1"
        style="width: 50%; margin-top: 15px"
        :tableHeader="tableHeader6"
        :tableData="GeneralTabularDataParmsTableDateList.dtos"
        :TableHeight="45"
        :outTableHeight="100"
        :isLoading="isLoading5"
      />
      <PageModule :PageData="{ total: totalLotGent }" @pageChange="handleSizeChangeTtotalLotGen" @pageSizeChange="handleCurrentChangeTtotalLotGen" />
    </el-drawer>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'StorageOtherMaterials'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ElMessage } from 'element-plus'
import { ref, onMounted, watch } from 'vue'
import {
  QueryTheStorageDetails,
  InstructionData,
  WaitingForStorageDetails,
  QueryOtherMaterialsInTheWarehouse,
  MaterialDetail,
  CommandMaterial,
  CommandMaterialEcho,
  GeneralMaterialEcho,
  GeneralMaterialsFormData,
  InstructionSubmaterial,
  StorageBatchProcessing,
  newDate,
  CompleteOrder
} from '@/api/WarehouseDepartment/FactoryStorage/StorageOtherMaterials'
import { ElMessageBox } from 'element-plus'
import type {
  InstructionSubmaterialParms,
  MaterialDetailParms,
  MaterialDetailInterface,
  WaitingForStorageDetailsInterface,
  QueryOtherMaterialsInTheWarehouseParms,
  QueryOtherMaterialsInTheWarehouseInterface,
  CommandMaterialParms,
  CommandMaterialInterface,
  GeneralMaterialsFormDataParms,
  GeneralMaterialsFormDataInterface,
  StorageBatchProcessingParms,
  newDateParms
} from '@/api/WarehouseDepartment/FactoryStorage/StorageOtherMaterials'
const formList1 = ref([
  {
    type: 'button',
    checkboxLabel: '通用材料'
  },
  {
    type: 'button',
    checkboxLabel: '指令材料'
  },
  {
    type: 'dateSingle',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '部门',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '入库原因',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '入库批处理'
  },
  {
    type: 'button',
    checkboxLabel: '打印'
  },
  {
    type: 'button',
    checkboxLabel: '打印(连打)'
  }
])

const formList5 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入入库单号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '入库原因',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '整单删除'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const formList = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '大类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '中类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '小类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '名称1',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '名称2',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '名称3',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '颜色',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '规格',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '单位',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const formList2 = ref([
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '货号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '通用材料'
  }
])

const formList3 = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '材料大类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '材料子类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const printQRCode = () => {
  const qrCodeContainer = document.querySelector<any>('.qrCodeContainer')
  if (qrCodeContainer) {
    // 临时显示 .qrCodeContainer 以获取其内容
    qrCodeContainer.style.display = 'block'

    const printWindow = window.open('', '_blank')
    printWindow!.document.write(`
      <!DOCTYPE html>
      <html>
        <head>
          <title>打印指令领料作业</title>
          <link rel="stylesheet" type="text/css" href="print.css" media="print">
          <style>
            body, html {
              margin: 0;
              padding: 0;
            }
            @page {
              size: auto; 
              margin: 3mm;
            }
            @media print {
                /* 打印时的样式 */
             
                
                /* 其他打印样式 */
            }
            h2 {
                    margin-left: initial; /* 或者设置适合打印的左边距 */
                    page-break-inside: avoid; /* 避免在h2元素内部分页 */
                }
               

            table, th, td {
                    border: 1px solid black; 
                    border-collapse: collapse; 
                    text-align: center;
                  }
                  th, td {
                    padding: 5px; /* 设置内边距 */
                    text-align: left; /* 设置文本对齐方式 */
                    text-align: center;
                    font-size:11px
                  }
                  .double-line {
                    border-top: 2px dashed red; 
                    border-bottom: 2px dashed red; 
                    text-align: center;
                  }
           
            /* 可以在这里添加更多自定义的打印样式 */
          </style>
        </head>
        <body>
    `)

    // 将 .qrCodeContainer 的内容复制到打印窗口的 body 中
    const cloneContainer = qrCodeContainer.cloneNode(true)
    printWindow!.document.body.appendChild(cloneContainer)

    printWindow!.document.write(`
        </body>
      </html>
    `)
    printWindow!.document.close()

    // 开始打印
    printWindow!.print()

    // 打印完成后，可以恢复 .qrCodeContainer 的原始显示状态
    qrCodeContainer.style.display = 'none'
  }
}

// 日期
const formatCurrentDateTime = (date?: Date) => {
  const year = date!.getFullYear()
  const month = String(date!.getMonth() + 1).padStart(2, '0') // 使用padStart确保月份是两位数
  const day = String(date!.getDate()).padStart(2, '0') // 使用padStart确保日期是两位数
  const hours = String(date!.getHours()).padStart(2, '0') // 小时
  const minutes = String(date!.getMinutes()).padStart(2, '0') // 分钟
  const seconds = String(date!.getSeconds()).padStart(2, '0') // 秒

  // 返回格式化的日期时间字符串
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

const currentDate = ref<any>(new Date())

// 双击后修改
const ChangeInputField = () => {
  TableListDetailed.value?.forEach((item: any) => {
    item.change = 'Y'
  })
}

// 修改后点击确定按钮
const confirmChange = async (row: any) => {
  const result = await LoginWork.put<any, any>(
    '/pcFactorStockQuery/otherMtlWaitStockUpdate',
    [row].map((item) => {
      return {
        mtlNo: item.mtlNo,
        qty: item.qty,
        price: item.price,
        amt: item.amt,
        remark: item.remark
      }
    })
  )
  if (result.code === 200) {
    JMessage.success(result.message)
    DetailedMethodOfTapeEntry()
  } else {
    JMessage.error(result.message)
  }
}

// 整单删除
const SingleDeleteMethodString = ref('')

const handleClose3 = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗')
    .then(() => {
      SingleDeleteMethod()
    })
    .catch(() => {})
}

const clicktogetdata5 = (row: any) => {
  const { slipNo } = row || {}
  SingleDeleteMethodString.value = slipNo
  slipNoList3.value = TableDateListOne.value.dtos.filter((item: any) => row.slipNo === item.slipNo)
}

const SingleDeleteMethod = async () => {
  const result = await CompleteOrder(SingleDeleteMethodString.value)
  if (result.code === 200) {
    JMessage.success(result.message)

    DetailedMethodsOfOtherMaterialsStorage()
  } else {
    JMessage.error(result.message)
  }
}

// 新增数据
const newDateListDabuleParms = ref<newDateParms>({
  lotNo: '',
  mtlNo: '',
  seqNo: ''
})

const typeString = ref(1)

const newDateListDabule = async () => {
  const result = await newDate(newDateListDabuleParms.value, typeString.value)
  if (result.code === 200) {
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 双击事件
const currentRow1 = (row: any) => {
  const { lotNo, mtlNo, seqNo } = row || {}
  newDateListDabuleParms.value.lotNo = lotNo
  newDateListDabuleParms.value.mtlNo = mtlNo
  newDateListDabuleParms.value.seqNo = seqNo
  newDateListDabule()
  DetailedMethodOfTapeEntry()
}

// 入库批量处理
const StorageBatchProcessingMethodParms = ref<any>([])

const IncomingFirm = ref({
  slipReason: '',
  deptNo: '',
  deptName: '',
  slipDate: ''
})

const clicktogetdata1 = (selectList: any) => {
  StorageBatchProcessingMethodParms.value = selectList.map((item: any) => {
    return {
      mtlNo: item.mtlNo,
      slipReason: IncomingFirm.value.slipReason,
      deptNo: IncomingFirm.value.deptNo,
      deptName: IncomingFirm.value.deptName,
      slipDate: IncomingFirm.value.slipDate
    }
  })
}

const StorageBatchProcessingMethod = async () => {
  const result = await StorageBatchProcessing(IncomingFirm.value!)
  if (result.code === 200) {
    JMessage.success(result.message)
    DetailedMethodOfTapeEntry()
  } else {
    JMessage.error(result.message)
  }
}

const clicktogetdata = (row: any) => {
  const { lotNo, mtlNo, seqNo } = row || {}
  FitSubmaterialDetailsParms.value.lotNo = lotNo
  FitSubmaterialDetailsParms.value.mtlNo = mtlNo
  FitSubmaterialDetailsParms.value.seqNo = seqNo

  FitSubmaterialDetails()
}

// 贴合子材料明细
const FitSubmaterialDetailsParms = ref<InstructionSubmaterialParms>({
  lotNo: '',
  mtlNo: '',
  seqNo: ''
})

const FitSubmaterialDetailsTableDateList = ref({
  dtos: []
})

const FitSubmaterialDetails = async () => {
  const result = await InstructionSubmaterial(FitSubmaterialDetailsParms.value)
  FitSubmaterialDetailsTableDateList.value.dtos = result.dtos
}

// 通用材料表格数据
const isLoading5 = ref(true)

const GeneralTabularDataParms = ref<GeneralMaterialsFormDataParms>({
  mtlName: '',
  mtlTypeL: '',
  mtlTypeS: '',
  pageNum: 1,
  pageSize: 40
})

const GeneralTabularDataParmsTableDateList = ref<GeneralMaterialsFormDataInterface>({
  dtos: []
})

const GeneralTabularData = async () => {
  const result = await GeneralMaterialsFormData(GeneralTabularDataParms.value)
  totalLotGent.value = result.total!
  GeneralTabularDataParmsTableDateList.value.dtos = result.dtos
  isLoading5.value = false
}

// 指令通用材料回显
const Command2 = ref<string>('')

const CommandGeneralMaterialEcho = async () => {
  const result = await GeneralMaterialEcho(Command2.value)
  let NewArray1 = []
  NewArray1 = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])
  formList3.value[0].selectOptions = EvaluatesTheArrayReturnValue(NewArray1)
}

const CommandGeneralMaterialEcho1 = async () => {
  const result = await GeneralMaterialEcho(Command2.value)
  let NewArray1 = []
  NewArray1 = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])
  formList3.value[1].selectOptions = EvaluatesTheArrayReturnValue(NewArray1)
}

watch(Command2, (newValue, oldValue) => {
  isLoading5.value = true
  CommandGeneralMaterialEcho1()
})

// 指令材料
const CommandMaterialEchoMethod = async () => {
  const result = await CommandMaterialEcho()
  let NewArray1 = []

  NewArray1 = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])
  formList2.value[2].selectOptions = EvaluatesTheArrayReturnValue(NewArray1)
}

const isLoading3 = ref(true)

const TableDateListInstructionMaterialMethod = ref<CommandMaterialInterface>({
  dtos: []
})

const InstructionMaterialMethodParms = ref<CommandMaterialParms>({
  carNo: '',
  lotNo: '',
  mtlName: '',
  pageNum: 1,
  pageSize: 40,
  typeNo: ''
})

const InstructionMaterialMethod = async () => {
  isLoading3.value = true
  const result = await CommandMaterial(InstructionMaterialMethodParms.value)
  TableDateListInstructionMaterialMethod.value.dtos = result.dtos
  totalLot.value = result.total!
  const { lotNo, mtlNo, seqNo } = result?.dtos[0] || {}
  FitSubmaterialDetailsParms.value.lotNo = lotNo
  FitSubmaterialDetailsParms.value.mtlNo = mtlNo
  FitSubmaterialDetailsParms.value.seqNo = seqNo as unknown as string
  FitSubmaterialDetails()
  isLoading3.value = false
}

// 通用材料明细
const isLoading2 = ref(true)

const GeneralMaterialDetailParms = ref<MaterialDetailParms>({
  mtlColor: '',
  mtlName_1: '',
  mtlName_2: '',
  mtlName_3: '',
  mtlSpec: '',
  mtlTypeL: '',
  mtlTypeM: '',
  mtlTypeS: '',
  mtlUnit: '',
  pageNum: 1,
  pageSize: 40
})

const TableDateListGeneral = ref<MaterialDetailInterface>({
  dtos: []
})

const GeneralMaterialDetail = async () => {
  const result = await MaterialDetail(GeneralMaterialDetailParms.value)
  totalGeneral.value = result.total!
  TableDateListGeneral.value.dtos = result.dtos
  isLoading2.value = false
}

// 其他材料入库明细
const isLoading1 = ref(false)

const DetailedMethodsOfOtherMaterialsStorageParms = ref<QueryOtherMaterialsInTheWarehouseParms>({
  lotNo: '',
  mtlName: '',
  pageNum: 1,
  pageSize: 40,
  slipDateEnd: '',
  slipDateStart: '',
  slipNo: '',
  slipReason: ''
})

const TableDateListOne = ref<QueryOtherMaterialsInTheWarehouseInterface>({
  dtos: []
})

const DetailedMethodsOfOtherMaterialsStorage = async () => {
  isLoading1.value = true
  const result = await QueryOtherMaterialsInTheWarehouse(DetailedMethodsOfOtherMaterialsStorageParms.value)
  TableDateListOne.value.dtos = result.dtos
  total.value = result.total!
  isLoading1.value = false
}

const isLoading = ref(true)

// 待入库明细
const TableListDetailed = ref<WaitingForStorageDetailsInterface[]>()

const DetailedMethodOfTapeEntry = async () => {
  isLoading.value = true
  const result = await WaitingForStorageDetails()
  TableListDetailed.value = result
  result.forEach((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
  })
  isLoading.value = false
}

// 指令材料回显
const Command = ref<string>('')

const Command1 = ref<string>('')

watch(Command, (newValue, oldValue) => {
  CommandMaterialEchoData1()
})

watch(Command1, (newValue, oldValue) => {
  CommandMaterialEchoData2()
})

const CommandMaterialEchoData = async () => {
  const result = await InstructionData(Command.value)
  let NewArray1 = []

  NewArray1 = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])
  formList.value[0].selectOptions = EvaluatesTheArrayReturnValue(NewArray1)
}

const CommandMaterialEchoData1 = async () => {
  isLoading2.value = true
  const result = await InstructionData(Command.value)
  let NewArray1 = []

  NewArray1 = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])
  formList.value[1].selectOptions = EvaluatesTheArrayReturnValue(NewArray1)
}

const CommandMaterialEchoData2 = async () => {
  isLoading2.value = true
  const result = await InstructionData(Command1.value)
  let NewArray1 = []

  NewArray1 = result.reduce((arr: any, item: any) => {
    if (item.typeNo && item.typeName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.typeNo + ' ' + item.typeName)
    }
    return arr
  }, [])
  formList.value[2].selectOptions = EvaluatesTheArrayReturnValue(NewArray1)
}

// 通用材料
const drawer = ref(false)

// 指令材料
const drawer1 = ref(false)

// 通用材料
const drawer2 = ref(false)

// 入库明细回显
const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const EntryDetailData = async () => {
  const result = await QueryTheStorageDetails()
  let NewArray = []
  let NewArray1 = []

  NewArray = result.depts.reduce((arr: any, item: any) => {
    if (item.deptName && item.deptNo) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.deptNo + ' ' + item.deptName)
    }
    return arr
  }, [])
  NewArray1 = result.vendors.reduce((arr: any, item: any) => {
    if (item.vendorNo && item.vendorName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.vendorNo + ' ' + item.vendorName)
    }
    return arr
  }, [])
  formList1.value[3].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  formList1.value[4].selectOptions = EvaluatesTheArrayReturnValue(NewArray1)
  formList1.value[5].selectOptions = EvaluatesTheArrayReturnValue(result.slipReason)
  formList5.value[1].selectOptions = EvaluatesTheArrayReturnValue(result.slipReason)
}

const slipNoList3 = ref<any>([])

// 统一调用接口
const interAceFunct = () => {
  //待入库明细
  EntryDetailData()

  DetailedMethodOfTapeEntry()

  DetailedMethodsOfOtherMaterialsStorage()
}

// 用户信息
const userInfo = ref()

onMounted(() => {
  interAceFunct()

  let userInf = sessionStorage.getItem('userInfo')

  userInfo.value = JSON.parse(userInf!)
})

// 表格数据

const tableHeader = ref([
  {
    label: '指令',
    prop: 'lotNo',
    change: true
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },
  {
    label: '数量',
    prop: 'qty'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '金额',
    prop: 'amt'
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '物料编码',
    prop: 'mtlNo',
    change: true
  }
])

const tableHeader1 = ref([
  {
    label: '入库单号',
    prop: 'slipNo'
  },
  {
    label: '入库日期',
    prop: 'slipDate'
  },
  {
    label: '入库原因',
    prop: 'slipReason'
  },
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '数量',
    prop: 'qty'
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '物料编号',
    prop: 'mtlNo'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '部门名称',
    prop: 'deptName'
  },
  {
    label: '金额',
    prop: 'amt'
  },
  {
    label: '仓管员',
    prop: 'sysUser'
  }
])

const tableHeader2 = ref([
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  }
])

const tableHeader3 = ref([
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '序号',
    prop: 'seqNo'
  },
  {
    label: '部位',
    prop: 'partName'
  },
  {
    label: '段',
    prop: 'segment'
  },
  {
    label: '分类',
    prop: 'mtlTypeNo'
  },
  {
    label: '分类名称',
    prop: 'mtlTypeNo'
  },
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlColor'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlSpec'
  },
  {
    label: '用量',
    prop: 'ttlUsage'
  }
])

const tableHeader5 = ref([
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '子材料名称',
    prop: 'mtlName'
  },
  {
    label: '子材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '比例',
    prop: 'percent'
  }
])

const tableHeader6 = ref([
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '材料类型',
    prop: 'mtlTypeNo'
  },
  {
    prop: 'mtlTypeName'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlName'
  },
  {
    label: '颜色',
    prop: 'mtlColor'
  },
  {
    label: '单位',
    prop: 'mtlNo'
  }
])

const Launchform5 = (row: any) => {
  DetailedMethodsOfOtherMaterialsStorageParms.value.slipNo = row[0].inputValue
  DetailedMethodsOfOtherMaterialsStorageParms.value.slipReason = row[1].selectValue

  DetailedMethodsOfOtherMaterialsStorageParms.value.slipDateStart = row[2].dateValue[0]
  DetailedMethodsOfOtherMaterialsStorageParms.value.slipDateEnd = row[2].dateValue[1]

  DetailedMethodsOfOtherMaterialsStorageParms.value.mtlName = row[3].inputValue
  DetailedMethodsOfOtherMaterialsStorageParms.value.lotNo = row[4].inputValue

  DetailedMethodsOfOtherMaterialsStorage()
}

const Launchform = (row: any) => {
  IncomingFirm.value.deptName = row[4].selectValue?.split(' ')[0]?.trim()
  IncomingFirm.value.deptNo = row[3].selectValue?.split(' ')[1]?.trim()
  IncomingFirm.value.slipReason = row[5].selectValue?.split(' ')[0]?.trim()
  IncomingFirm.value.slipDate = row[2].dateValue
}

const Launchform1 = (row: any) => {
  if (row[0].selectValue?.split(' ')[0]?.trim() !== '') {
    Command.value = row[0].selectValue?.split(' ')[0]?.trim()
  }
  if (row[1].selectValue?.split(' ')[0]?.trim() !== '') {
    Command1.value = row[1].selectValue?.split(' ')[0]?.trim()
  }
  if (row[0].selectValue?.split(' ')[0]?.trim() === '') {
    isLoading2.value = true
  }

  GeneralMaterialDetailParms.value.mtlTypeL = row[0].selectValue?.split(' ')[0]?.trim()
  GeneralMaterialDetailParms.value.mtlTypeM = row[1].selectValue?.split(' ')[0]?.trim()
  GeneralMaterialDetailParms.value.mtlTypeS = row[2].selectValue?.split(' ')[0]?.trim()
  GeneralMaterialDetailParms.value.mtlName_1 = row[3].inputValue
  GeneralMaterialDetailParms.value.mtlName_2 = row[4].inputValue
  GeneralMaterialDetailParms.value.mtlName_3 = row[5].inputValue
  GeneralMaterialDetailParms.value.mtlColor = row[6].inputValue
  GeneralMaterialDetailParms.value.mtlSpec = row[7].inputValue
  GeneralMaterialDetailParms.value.mtlUnit = row[8].inputValue
  GeneralMaterialDetail()
}

const Launchform2 = (row: any) => {
  InstructionMaterialMethodParms.value.lotNo = row[0].inputValue
  InstructionMaterialMethodParms.value.carNo = row[1].inputValue
  InstructionMaterialMethodParms.value.typeNo = row[2].selectValue?.split(' ')[0]?.trim()
  InstructionMaterialMethodParms.value.mtlName = row[3].inputValue
  InstructionMaterialMethod()
}

const findClicked = (find: any) => {
  if (find === '通用材料') {
    typeString.value = 1
    CommandMaterialEchoData()
    drawer1.value = true
  }
  if (find === '指令材料') {
    typeString.value = 2
    CommandMaterialEchoMethod()
    drawer.value = true
  }
  if (find === '入库批处理') {
    StorageBatchProcessingMethod()
  }
  if (find === '整单删除') {
    handleClose3()
  }
  if (find === '置空') {
    clearFormList1()
  }
  if (find === '打印') {
    console.log(slipNoList3.value)
    if (slipNoList3.value.length === 0) {
      JMessage.warning('请点击需要打印的数据')
    } else if (slipNoList3.value[0].slipNo === null) {
      JMessage.warning('请选择有出库单号的进行打印')
    } else {
      printQRCode()
    }
  }
  if (find === '打印(连打)') {
    console.log(slipNoList3.value)
    if (slipNoList3.value.length === 0) {
      JMessage.warning('请点击需要打印的数据')
    } else if (slipNoList3.value[0].slipNo === null) {
      JMessage.warning('请选择有出库单号的进行打印')
    } else {
      printQRCode()
    }
  }
}

const findClicked1 = (find: any) => {
  if (find === '置空') {
    isLoading2.value = true
    clearFormList3()
  }
}

const findClicked2 = (find: any) => {
  if (find === '置空') {
    clearFormList5()
  }
  if (find === '通用材料') {
    drawer2.value = true
    typeString.value = 3
    CommandGeneralMaterialEcho()
    GeneralTabularData()
  }
}

const clearFormList3 = () => {
  formList.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    // Add other conditions for different form item types
  })
}

const clearFormList5 = () => {
  formList2.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    // Add other conditions for different form item types
  })
}

const clearFormList1 = () => {
  formList5.value.forEach((item) => {
    if (item.type === 'select') {
      item.selectValue = ''
    }
    if (item.type === 'date') {
      item.dateValue = ''
    }
    if (item.type === 'input') {
      item.inputValue = ''
    }
    // Add other conditions for different form item types
  })
}

const clearFormList2 = () => {
  formList3.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
    // Add other conditions for different form item types
  })
}

const Launchform3 = (row: any) => {
  if (row[0].selectValue?.split(' ')[0]?.trim() !== '') {
    Command2.value = row[0].selectValue?.split(' ')[0]?.trim()
  }
  if (row[0].selectValue?.split(' ')[0]?.trim() === '') {
    isLoading5.value = true
  }
  GeneralTabularDataParms.value.mtlTypeL = row[0].selectValue?.split(' ')[0]?.trim()
  GeneralTabularDataParms.value.mtlTypeS = row[1].selectValue?.split(' ')[0]?.trim()
  GeneralTabularDataParms.value.mtlName = row[2].inputValue
  GeneralTabularData()
}

const findClicked3 = (find: any) => {
  if (find === '置空') {
    clearFormList2()
  }
}

// 分页器
const total = ref(0)

const handleSizeChangeT = (val: number) => {
  isLoading1.value = true
  DetailedMethodsOfOtherMaterialsStorageParms.value.pageNum = val
  DetailedMethodsOfOtherMaterialsStorage()
}

const handleCurrentChangeT = (val: number) => {
  isLoading1.value = true
  DetailedMethodsOfOtherMaterialsStorageParms.value.pageSize = val
  DetailedMethodsOfOtherMaterialsStorage()
}

// 通用材料分页器分页器
const totalGeneral = ref(0)

const handleSizeChangeTGeneral = (val: number) => {
  isLoading2.value = true
  GeneralMaterialDetailParms.value.pageNum = val
  GeneralMaterialDetail()
}

const handleCurrentChangeTGeneral = (val: number) => {
  isLoading2.value = true
  GeneralMaterialDetailParms.value.pageSize = val
  GeneralMaterialDetail()
}

// 指令材料材料分页器分页器
const totalLot = ref(0)

const handleSizeChangeTtotalLot = (val: number) => {
  InstructionMaterialMethodParms.value.pageNum = val
  InstructionMaterialMethod()
}

const handleCurrentChangeTtotalLot = (val: number) => {
  InstructionMaterialMethodParms.value.pageSize = val
  InstructionMaterialMethod()
}

// 通用材料材料分页器分页器
const totalLotGent = ref(0)

const handleSizeChangeTtotalLotGen = (val: number) => {
  isLoading5.value = true
  GeneralTabularDataParms.value.pageNum = val
  GeneralTabularData()
}

const handleCurrentChangeTtotalLotGen = (val: number) => {
  isLoading5.value = true
  GeneralTabularDataParms.value.pageSize = val
  GeneralTabularData()
}

// 转入仓库
// watch(IncomingFirm.value, (newInputValue, oldInputValue) => {
//   if (IncomingFirm.value.deptName !== '' || IncomingFirm.value.deptNo !== undefined || IncomingFirm.value.slipReason !== '') {
//     // 将对象属性按照索引排序
//     const sortedKeys = Object.values(TableListDetailed.value as any)

//     // 遍历排序后的属性值
//     sortedKeys.forEach((key: any) => {
//       key.change = ''
//     })
//   }
// })
</script>

<style lang="less" scoped>
h1 {
  font-size: 16px;
}
</style>
